package com.spx.core.services.security;

import java.lang.reflect.Method;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

import com.spx.core.CoreActivator;
import com.spx.core.services.ActivityLabel;
import com.spx.core.types.Activity;
import com.spx.core.types.User;

public class AuthorityServiceImpl implements AuthorityService, org.springframework.aop.MethodBeforeAdvice
{

	private void addctivityJob(final String activityName)
	{
		Job job = new Job("My First Job")
		{

			@Override
			protected IStatus run(IProgressMonitor monitor)
			{
				System.out.println("Hello World (from a background job)");
				Activity newActivity = new Activity();
				newActivity.setName(activityName);
				 CoreActivator.getDefault().getSecurityService().addAction(newActivity);
				return Status.OK_STATUS;
			}
		};
		job.setPriority(Job.SHORT);
		job.schedule(1000);
	}

	@Override
	@Deprecated
	public void checkPerform(Activity activity)
	{
	//	User presentUser = CoreActivator.getDefault().getSecurityService().getPresentUser();

	}

	@Override
	public void before(Method method, Object[] arg1, Object arg2) throws Throwable
	{

		// new name for Actions
		ActivityLabel activityLabel = method.getAnnotation(ActivityLabel.class);
		if (activityLabel != null)
		{

			User presentUser = CoreActivator.getDefault().getSecurityService().getPresentUser();
			if (presentUser != null && StringUtils.equals(presentUser.getName(), "root"))
			{
				return;
			}

			Activity presentActivity = CoreActivator.getDefault().getSecurityService().findActivity(activityLabel.name());
			if (presentActivity == null)
			{
				addctivityJob(activityLabel.name());
				NoSuchActivityException ae = new NoSuchActivityException();
				ae.setActivityName(activityLabel.name());
				throw ae;
			}

			// User must have logged in
			if (presentUser == null)
			{
				throw new NoUserException();
			}
			if (presentUser.canPerform(presentActivity.getName()) == false)
			{

				UnAuthorisedActivityException uae = new UnAuthorisedActivityException();
				uae.setUser(presentUser);
				uae.setActivity(presentActivity);
				throw uae;
			}

		}

	}

}
